﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Rally_like_a_madman.Data;
using System.Collections.ObjectModel;

namespace Rally_like_a_madman
{
    public partial class StartRoutePage : PhoneApplicationPage
    {
        public ObservableCollection<Route> routes;

        public StartRoutePage()
        {
            InitializeComponent();

            App thisApp = Application.Current as App;
            var routes = from Route route in thisApp.ActiveDB.RouteTable select route;

            this.routes = new ObservableCollection<Route>(routes.ToList() as List<Route>);

            lbAllRoutes.ItemsSource = this.routes;
        }

        private void btnStartRoute_Click(object sender, RoutedEventArgs e)
        {
            Route selectedRoute = lbAllRoutes.SelectedItem as Route;

            if (selectedRoute == null)
                return;

            if (NavigationHelper.StartRoute(selectedRoute))
                NavigationService.Navigate(new Uri("/NavigationPage.xaml", UriKind.RelativeOrAbsolute));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemoveFromDatabase_Click(object sender, RoutedEventArgs e)
        {
            Route selectedRoute = lbAllRoutes.SelectedItem as Route;

            MessageBoxResult m = MessageBox.Show("Weet je zeker dat je de geselecteerde Route (" + selectedRoute.Name
                + ") uit de database wil verwijderen?\n\nDit is definitief en kan niet ongedaan gemaakt worden.",
                "Route verwijderen uit database", MessageBoxButton.OKCancel);

            if (m == MessageBoxResult.OK)
            {
                App thisApp = Application.Current as App;

                #region Remove RouteWaypointLinks from database
                //retrieve all existing RouteWayPointLinks
                var routeWaypointLinksDB = from RouteWaypointLink routeWaypointLink in thisApp.ActiveDB.RouteLinkTable select routeWaypointLink;
                //convert the var to a List<RouteWaypointLink>
                List<RouteWaypointLink> existingRouteWaypointLinks = routeWaypointLinksDB.ToList() as List<RouteWaypointLink>;

                //delete every RouteWaypointLink where the IDRoute is the same as the selected IDRoute
                foreach (RouteWaypointLink r in existingRouteWaypointLinks)
                {
                    if (r.Route.IDRoute == selectedRoute.IDRoute)
                        thisApp.ActiveDB.RouteLinkTable.DeleteOnSubmit(r);
                }
                #endregion

                #region Remove Scores from database
                //retrieve all existing RouteWayPointLinks
                var scoresDB = from Score score in thisApp.ActiveDB.ScoreTable select score;
                //convert the var to a List<Score>
                List<Score> existingScoreLinks = scoresDB.ToList() as List<Score>;

                //delete every Score where the IDRoute is the same as the selected IDRoute
                foreach (Score s in existingScoreLinks)
                {
                    if (s.Route.IDRoute == selectedRoute.IDRoute)
                        thisApp.ActiveDB.ScoreTable.DeleteOnSubmit(s);
                }
                #endregion

                #region Remove Route from database
                //retrieve all existing Routes
                var routeDB = from Route route in thisApp.ActiveDB.RouteTable select route;
                //convert the var to a List<Route>
                List<Route> existingRoutes = routeDB.ToList() as List<Route>;

                //delete every Route where the IDRoute is the same as the selected IDRoute
                foreach (Route r in existingRoutes)
                {
                    if (r.IDRoute == selectedRoute.IDRoute)
                        thisApp.ActiveDB.RouteTable.DeleteOnSubmit(r);
                }

                thisApp.ActiveDB.SubmitChanges();
                #endregion

                //Remove selecedRoute from the ObservableCollection
                routes.Remove(selectedRoute);
            }
        }
    }
}